package net.miranti.opmov;

import java.util.ArrayList;
import java.util.List;

import net.miranti.dao.GenericDao;
import net.miranti.opmov.OperationalMovement.Type;
import net.miranti.opmov.client.Airport;
import net.miranti.opmov.client.BtdService;
import net.miranti.opmov.client.OpMovBean;

import com.google.inject.Inject;

public class BtdServiceImpl implements BtdService {
	@Inject GenericDao dao;
	
	@Override
	public Airport[] getAirports() {
		return dao.findAll(Airport.class).toArray(new Airport[0]);
	}

	public OpMovBean[] search(String airport, 
			Integer segment, Integer variable, Integer periodicity) {
		List<OpMovBean> list = new ArrayList();
		
		if(airport != null){
			List<OperationalMovement> rawList = 
				dao.namedQuery(OperationalMovement.FIND_BY_AIRPORT, variable);
			
			for(OperationalMovement om : rawList){
				String p = om.getPeriod();
				Type v = om.getType();
				Integer d = om.getDomestic();
				Integer i = om.getInternational();
				
				list.add(new OpMovBean(p, v, d, i));
			}
		} else {
			List<Object[]> aggregateList = 
				dao.namedQuery(OperationalMovement.FIND_ALL_AIRPORTS);
			
			for(Object[] o : aggregateList){
				String p = (String) o[0];
				Type v = (Type) o[1];
				Long d = (Long) o[2];
				Long i = (Long) o[3];
				
				list.add(new OpMovBean(p, v, d.intValue(), i.intValue()));
			}
		}
			
		return list.toArray(new OpMovBean[0]);
	}

	@Override
	public OpMovBean[] search(String airport) {
		return null;
	}
}
